/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.unal.agentes;

import co.edu.unal.behaviours.CreateStock;
import co.edu.unal.behaviours.RequestPerformer;
import co.edu.unal.gui.BookBuyerGui;
import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.Behaviour;
import jade.core.behaviours.TickerBehaviour;
import jade.domain.DFService;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.domain.FIPAException;

/**
 *
 * @author raider
 */
public class BookBuyerAgent extends Agent {
    public Behaviour comp;
// The title of the book to buy
    private String targetBookTitle;
// The list of known seller agents
    private AID[] sellerAgents;    
    private BookBuyerGui myGui;
    // Put agent initializations here
    @Override
    protected void setup() {
        //Creando gui
        myGui = new BookBuyerGui(this);
        myGui.showGui();
        // Printout a welcome message
        myGui.getLog().setText("Hello! Buyer-agent " + getAID().getLocalName() + " is ready.");
        //Definiendo comportamiento
        comp = new TickerBehaviour(this, 5000) {
                @Override
                protected void onTick() {
                    // Update the list of seller agents
                    DFAgentDescription template = new DFAgentDescription();
                    ServiceDescription sd = new ServiceDescription();
                    sd.setType("book-seller");
                    template.addServices(sd);
                    try {
                        DFAgentDescription[] result = DFService.search(myAgent, template);
                        sellerAgents = new AID[result.length];
                        for (int i = 0; i < result.length; ++i) {
                            sellerAgents[i] = result[i].getName();
                        }
                    } catch (FIPAException fe) {
                        myGui.getLog().setText(myGui.getLog()+"\n"+"fe.getMessage() = " + fe.getMessage());
                    }
                    myAgent.addBehaviour(new RequestPerformer(sellerAgents, targetBookTitle,"1",myGui.getLog()));
                }
            } ;
        // Get the title of the book to buy as a start-up argument
        Object[] args = getArguments();
        if (args != null && args.length > 0) {
            targetBookTitle = (String) args[0];
            myGui.getLog().setText(myGui.getLog().getText()+"\n"+"Trying to buy " + targetBookTitle);
            // Add a TickerBehaviour that schedules a request to seller agents every minute
            addBehaviour(comp);
        } else {
// Make the agent terminate immediately
            myGui.getLog().setText(myGui.getLog()+"\n"+"No book title specified");
            doDelete();
        }
    }
// Put agent clean-up operations here
    @Override
    protected void takeDown() {
// Printout a dismissal message
        myGui.getLog().setText(myGui.getLog()+"\n"+"Buyer-agent " + getAID().getLocalName() + " terminating.");
    }

    public String getTargetBookTitle() {
        return targetBookTitle;
    }

    public void setTargetBookTitle(String targetBookTitle) {
        this.targetBookTitle = targetBookTitle;
    }
}
